package com.life.rule;

import java.awt.Color;
import java.awt.Dimension;

import com.life.constants.Constants;
import com.life.model.LifeModel;
import com.life.view.ColorGenerator;

public class SBLifeRule implements ILifeRule {

	protected int iteration = 0;
	
	/* stays alive if it has these values of neighbors */
	protected String sVals = Constants.DEFAULT_S_VALUES;
	
	/* born if has this many neighbors*/
	protected String bVals = Constants.DEFAULT_B_VALUES;
	
	protected String theName = "";
	
	protected ColorGenerator theColorGenerator = null;
	
	public SBLifeRule()
	{
		setColorGenerator(ColorGenerator.getInstance());
	}
	
	public SBLifeRule(String s, String b)
	{
		sVals = s;
		bVals = b;
		setColorGenerator(ColorGenerator.getInstance());
	}
	
	public SBLifeRule(String s,String b, ColorGenerator generator)
	{
		sVals = s;
		bVals = b;
		setColorGenerator(generator);
	}
	
	public SBLifeRule(String s,String b, ColorGenerator generator, String name)
	{
		sVals = s;
		bVals = b;
		setColorGenerator(generator);
		theName = name;
	}
	
	public LifeModel runRule(LifeModel model) {
		// TODO Auto-generated method stub
		LifeModel clonedModel = model.deepCloneModel();
		
		Dimension dim = model.getSize();
		int width = (int)dim.getWidth();
		int height = (int)dim.getHeight();
		
		for(int i = 0; i < width; i++)
		{
			for(int j = 0; j < height;j++)
			{
				int neighbors = model.getNeighborCount(i,j);
			
				if(model.isAlive(i,j))
				{
					/* check if it can stay alive */
					if(!canStayAlive(neighbors))
					{
						clonedModel.setDead(i,j);
					}
				}
				else
				{
					if(canCreateLife(neighbors))
					{
						clonedModel.setAlive(i,j);
					}
				}
			}
		}
		
		iteration++;
		return clonedModel;
		
	}
	
	protected boolean canStayAlive(int neighbors)
	{
		String neighborsStr = String.valueOf(neighbors);
		if(sVals.indexOf(neighborsStr) != -1)
		{
			return true;
		}
		
		return false;
	}
	
	protected boolean canCreateLife(int neighbors)
	{
		String neighborsStr = String.valueOf(neighbors);
		if(bVals.indexOf(neighborsStr) != -1)
		{
			return true;
		}
		
		return false;
	}
 
	public Color getNeighborColor(int neighbors) {
		// TODO Auto-generated method stub
		return theColorGenerator.getNeighborColor(neighbors);
	}

	public ColorGenerator getColorGenerator() {
		return theColorGenerator;
	}

	public void setColorGenerator(ColorGenerator theColorGenerator) {
		this.theColorGenerator = theColorGenerator;
	}
	
	public void setName(String val)
	{
		theName = val;
	}
	
	public String getRuleString()
	{
	    return "(" + sVals + "/" + bVals + ")";
	}
	
	public String toString()
	{
	    return theName;
	}

	public int getIteration() {
		// TODO Auto-generated method stub
		return iteration;
	}
	
	public void reset()
	{
		iteration = 0;
	}
	
	public ILifeRule deepRuleCopy()
	{
	    return new SBLifeRule(sVals,bVals,theColorGenerator,theName);
	}
	

}
